#pragma once
#include "BaseApp.h"
#include "../resource.h"

class WeGame : public BaseApp
{
public:
	WeGame(InputEntry& input) : BaseApp(input)
	{
		//_M_desc = _WIN_DESC_(L"wegame.exe", L"WeGame");
		_M_desc = _WIN_DESC_(L"tgp_daemon.exe", L"WeGame");
		ImageMatch::loadTargetImage(IDB_BITMAP7, &_M_img_approach);
		ImageMatch::loadTargetImage(IDB_BITMAP13, &_M_img_edits);
	}

	~WeGame()
	{
		delete _M_img_approach.pits;
		delete _M_img_edits.pits;
	}       

	StateCode prepare(HWND& hwnd) override
	{

		RECT rect;
		if (!GetWindowRect(hwnd, &rect))
		{
			return ERR_GETRECT_FAIL;
		}

		POINT pt;
		int retry = 0;
		while (retry++ < 60)
		{
			if (g_mch->findTargetImg(_M_img_approach, pt, 0.08))
			{
				_M_input->setCursorPosition(pt.x + 10, pt.y + 10);
				Sleep(50);
				_M_input->mouseClick();
				Sleep(500);
				if (g_mch->findTargetImg(_M_img_edits, pt))
				{
					return ST_SUCCESS;
				}
				else
				{
					printf("[LN]select qq, but doesn't work!\n");
					break;
				}
			}

			//if (g_mch->findTargetImg(_M_img_qrcode, pt))
			//{
			//	_M_input->setCursorPosition(pt.x + 64, pt.y + 310);
			//	Sleep(50);
			//	_M_input->mouseClick();
			//	Sleep(500);
			//	if (g_mch->findTargetImg(_M_img_login, pt))
			//	{
			//		return ST_SUCCESS;
			//	}
			//	else
			//	{
			//		printf("[ATL]select qq, but doesn't work!\n");
			//		break;
			//	}
			//}
			Sleep(1000);
		}
		return ERR_IMAGE_NOTFOUND;
	}

	StateCode input(HWND& hwnd) override
	{
		RECT rect;
		if (!GetWindowRect(hwnd, &rect) || (rect.right < 0 && rect.bottom < 0))
		{
			return ERR_GETRECT_FAIL;
		}
		int retry = 0;
		bool res = false;
		while (retry++ < 3)
		{
			res = g_mch->findTargetImg(_M_img_edits, _M_pt_edits);
			if (res)
			{
				break;
			}
			Sleep(1000);
		}
		if (!res)
		{
			return ERR_IMAGE_NOTFOUND;
		}
		_M_input->setCursorPosition(_M_pt_edits.x + 132, _M_pt_edits.y + 9);
		Sleep(50);
		_M_input->mouseClick();
		Sleep(10);
		_M_input->mouseClick();
		Sleep(50);
		int i = 0;
		for (; i < _M_account._M_NAME.size(); ++i)
		{
			_M_input->sendCharater(_M_account._M_NAME[i]);
			Sleep(50);
		}

		_M_input->setCursorPosition(_M_pt_edits.x + 132, _M_pt_edits.y + 38);
		Sleep(50);
		_M_input->mouseClick();
		Sleep(50);

		for (i = 0; i < _M_account._M_PASSWD.size(); ++i)
		{
			_M_input->sendCharater(_M_account._M_PASSWD[i]);
			Sleep(50);
		}
		return ST_SUCCESS;
	}

	StateCode login(HWND& hwnd) override
	{ 
		RECT rect;
		if (!GetWindowRect(hwnd, &rect))
		{
			return ERR_GETRECT_FAIL;
		}

		_M_input->setCursorPosition(_M_pt_edits.x + 80, _M_pt_edits.y + 120);
		Sleep(50);
		_M_input->mouseClick();
		Sleep(50);
		return ST_SUCCESS; 
	}
private:
	_IMG_RAW_		_M_img_approach;
	_IMG_RAW_		_M_img_edits;
	_IMG_RAW_		_M_img_qrcode;
	POINT			_M_pt_edits;
};